package org.yoyo.classroom.util;

import org.yoyo.classroom.domain.po.BookingDTO;
import org.yoyo.classroom.domain.po.BookingOrder;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class BookingConverter {

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");

    /**
     * 将 BookingDTO 转换为 BookingOrder
     *
     * @param bookingDTO 包含预约信息的数据传输对象
     * @return 转换后的 BookingOrder 对象
     * @throws ParseException 如果日期或时间格式不正确，则抛出异常
     */
    public BookingOrder convertToBookingOrder(BookingDTO bookingDTO) throws ParseException {
        BookingOrder bookingOrder = new BookingOrder();
        bookingOrder.setUserId(bookingDTO.getUserId());
        bookingOrder.setRoomId(bookingDTO.getRoomId());

        // 处理日期和时间
        Date bookingDate = DATE_FORMAT.parse(bookingDTO.getBookingDate());
        Date startTime = DATE_FORMAT.parse(bookingDTO.getBookingDate() + " " + bookingDTO.getStartTime());
        Date endTime = DATE_FORMAT.parse(bookingDTO.getBookingDate() + " " + bookingDTO.getEndTime());

        bookingOrder.setStartTime(startTime);
        bookingOrder.setEndTime(endTime);

        // 设置默认值
        bookingOrder.setStatus(0); // 假设 0 表示待处理状态
        bookingOrder.setAmount(BigDecimal.ZERO); // 假设初始金额为 0
        bookingOrder.setPaymentStatus(0); // 假设 0 表示未支付状态
        bookingOrder.setCreateTime(new Date()); // 当前时间
        bookingOrder.setUpdateTime(new Date()); // 当前时间
        bookingOrder.setIsDeleted(0); // 假设 0 表示未删除

        // 关联字段设置为空或默认值
        bookingOrder.setOrderNumber(""); // 订单号可以后续生成
        bookingOrder.setOrderTime(DATE_FORMAT.format(bookingDate)); // 订单时间
        bookingOrder.setOrderStatus("待处理"); // 订单状态
        bookingOrder.setUserName(""); // 预约人名称可以后续填充
        bookingOrder.setRoomName(""); // 房间名称可以后续填充
        bookingOrder.setSeatId(bookingDTO.getSeatId());

        return bookingOrder;
    }
}
